package main

import (
	"fmt"
	"manager/model"
	"manager/service"
)

type customerView struct {
	key             string
	loop            bool
	customerService *service.CustomerService
}

func (this *customerView) list() {
	custimers := this.customerService.List()
	fmt.Println("--------------客户列表--------------")
	for i := 0; i < len(custimers); i++ {
		fmt.Println(custimers[i].GetInfo())
	}
	fmt.Println("--------------客户列表完成--------------")
}

func (this *customerView) add() {
	fmt.Println(fmt.Println("--------------添加--------------"))
	fmt.Print("姓名:")
	name := ""
	fmt.Scanln(&name)
	customer := model.NewCustomer2(name)
	if this.customerService.Add(customer) {
		fmt.Println("--------------添加完成--------------")
	} else {
		fmt.Println("--------------添加失败--------------")
	}
}

func (this *customerView) delete() {
	fmt.Println("--------------删除客户--------------")
	fmt.Print("请选择需要删除的客户编号(-1退出)")
	id := -1
	fmt.Scanln(&id)
	if id == -1 {
		return
	}
	fmt.Println("缺人删除(Y/N):")
	choice := ""
	fmt.Scanln(&choice)
	if choice == "y" || choice == "Y" {
		if this.customerService.Delete(id) {
			fmt.Println("--------------删除完成--------------")
		} else {
			fmt.Println("--------------删除失败，输入的id不存在--------------")
		}
	}
}

func (this *customerView) exit() {
	fmt.Println("缺人是否退出(Y/N):")
	for {
		fmt.Scanln(&this.key)
		if this.key == "Y" || this.key == "y" || this.key == "N" || this.key == "n" {
			break
		}
		fmt.Print("你的输入有误，确认是否退出(Y/N)：")
	}
	if this.key == "Y" || this.key == "y" {
		this.loop = false
	}
}

func (this *customerView) mainMenu() {
	for {
		fmt.Println("-----------------客户信息管理软件-----------------")
		fmt.Println("                 1 添 加 客 户")
		fmt.Println("                 2 修 改 客 户")
		fmt.Println("                 3 删 除 客 户")
		fmt.Println("                 4 客 户 列 表")
		fmt.Println("                 5 退       出")
		fmt.Print("请选择(1-5)：")

		fmt.Scanln(&this.key)
		switch this.key {
		case "1":
			this.add()
		case "2":
			fmt.Println("修 改 客 户")
		case "3":
			this.delete()
		case "4":
			this.list()
		case "5":
			this.exit()
		default:
			fmt.Println("你的输入有误，请重新输入...")
		}

		if !this.loop {
			break
		}

	}
	fmt.Println("已退出了客户关系管理系统...")
}

func main() {
	customerView := customerView{key: "", loop: true}
	customerView.customerService = service.NewCustomerService()
	customerView.mainMenu()
}
